package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentMemberDemandBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MaterialFilterJson;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.entity.MemberCompanyTeamBean;
import com.sgb.member.entity.MemberGoodsListEntity;
import com.sgb.member.entity.MemberLeaseBean;
import com.sgb.member.ui.activity.MemberBusinessPriceActivity;
import com.sgb.member.ui.activity.MemberBusinessPriceActivity2;
import com.sgb.member.ui.activity.MemberBusinessPriceActivity3;
import com.sgb.member.ui.adapter.CompanyWebBusinessesAdapter;
import com.sgb.member.view_model.CompanyWebBusinessModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * 会员企业网站-调整经营
 */
public class CompanyWebBusinessFragment extends AppLazyFragment {

    @Inject
    CompanyWebBusinessModel sModel;
    private FragmentMemberDemandBinding mBinding;
    private CompanyWebBusinessesAdapter<MemberGoodsListEntity> mBusinessAdapter;
    private CompanyWebBusinessesAdapter<MemberLeaseBean> mLeaseAdapter;
    private CompanyWebBusinessesAdapter<MemberCompanyTeamBean> mTeamAdapter;

    private MemberBean memberBean;
    private int mCurPos;
    private String mNo1;
    private String mNo2;
    private String mNo3;
    private String flag;
    public static CompanyWebBusinessFragment newInstance(int position, MemberBean companyNo) {
        CompanyWebBusinessFragment fragment = new CompanyWebBusinessFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        bundle.putSerializable("COMPANY_NO", companyNo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_member_demand, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mCurPos = getArguments().getInt("INDEX", 0);
        memberBean = (MemberBean) getArguments().getSerializable("COMPANY_NO");
        flag= memberBean.getMemberUpdateLeverType();//1供方会员 查价 2需方会员 调价
        mBinding.tabLayout.setVisibility(View.GONE);
        initRecycler();
    }

    @Override
    protected void onLazyLoad() {
        loadOtherData();
        loadData(false);
    }


    private void loadOtherData() {
        switch (mCurPos) {
            case 0:
                sModel.getAllCategory(); // 行业分类
                break;
            case 1:
                sModel.getAllLeaseType(); // 行业分类
                break;
            case 2:
                sModel.getAllTeamType(); // 行业分类
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData(false);
    }

    private void initRecycler() {
        assert getArguments() != null;
        memberBean = (MemberBean) getArguments().getSerializable("COMPANY_NO");
        mCurPos = getArguments().getInt("INDEX", 0);
        mBinding.recyclerView.setPullRefreshEnabled(true);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
        boolean isDemandMember = TextUtils.equals("2", memberBean.getMemberUpdateLeverType()); //1供方会员 2需方会员
        switch (mCurPos) {
            case 0: // "经营商品"
                mBusinessAdapter = new CompanyWebBusinessesAdapter<>(getActivity(), R.layout.item_company_web_adjustment, null, isDemandMember);
                mBinding.recyclerView.setAdapter(mBusinessAdapter);
                mBusinessAdapter.setOnItemClick(new AdapterOnItemClick<MemberGoodsListEntity>() {
                    @Override
                    public void onItemClick(MemberGoodsListEntity bean, int position) {
                        // 商砼 == 7
                        if ("7".equals(bean.getCid1())) {
                            MaterialFilterJson filterJson = new MaterialFilterJson();
                            filterJson.setStrengthId(bean.getSort());
                            MemberConfig.getInstance().startConcreteDetail(getActivity(), bean.getId(), filterJson);
                        } else {
                            MemberConfig.getInstance().startShopGoodsDetail(getContext(), bean.getId(), "", bean.getSkuid());
                        }
                    }
                });
                mBusinessAdapter.setModifyQuoteListener(new CompanyWebBusinessesAdapter.ModifyQuoteListener<MemberGoodsListEntity>() {
                    @Override
                    public void onModifyQuote(int position, MemberGoodsListEntity entity) {
                        if (getActivity() != null) {
                            Bundle bundle = new Bundle();
                            bundle.putParcelable("bean", entity);
                            bundle.putString("no", memberBean.getCompanyNo());
                            MemberBusinessPriceActivity.Companion.jumptToAct(requireActivity(), bundle);
//                            new XPopup.Builder(getContext()).asCustom(new MemberBusinessPopup(getActivity(), new MemberBusinessPopup.OnConfirmListener() {
//                                @Override
//                                public void onConfirm() {
//                                    new XPopup.Builder(getContext()).asCustom(new MemberBusinessPricePopup(getActivity(), mCurPos, entity, new MemberBusinessPricePopup.OnConfirmListener() {
//                                        @Override
//                                        public void onConfirm(Object o) {
//                                            sModel.updateQuotePrice(mCurPos, memberBean.getCompanyNo(), entity, (String) o, position);
//                                        }
//                                    })).show();
//                                }
//                            })).show();
                        }
                    }
                });
                break;
            case 1: // "租赁经营"
                mLeaseAdapter = new CompanyWebBusinessesAdapter<>(getActivity(), R.layout.item_company_web_lease, null, isDemandMember);
                mBinding.recyclerView.setAdapter(mLeaseAdapter);
                mLeaseAdapter.setOnItemClick(new AdapterOnItemClick<MemberLeaseBean>() {
                    @Override
                    public void onItemClick(MemberLeaseBean bean, int position) {
                        sModel.getH5Url(bean.getType(), bean.getNo());
                    }
                });
                mLeaseAdapter.setModifyQuoteListener(new CompanyWebBusinessesAdapter.ModifyQuoteListener<MemberLeaseBean>() {
                    @Override
                    public void onModifyQuote(int position, MemberLeaseBean entity) {
                        if (getActivity() != null) {
                            Bundle bundle = new Bundle();
                            bundle.putParcelable("bean", entity);
                            bundle.putString("no", memberBean.getCompanyNo());
                            MemberBusinessPriceActivity2.Companion.jumptToAct(requireActivity(), bundle);
//                            new XPopup.Builder(getContext()).asCustom(new MemberBusinessPopup(getActivity(), new MemberBusinessPopup.OnConfirmListener() {
//                                @Override
//                                public void onConfirm() {

//                                    new XPopup.Builder(getContext()).asCustom(new MemberBusinessPricePopup(getActivity(), mCurPos, entity, new MemberBusinessPricePopup.OnConfirmListener() {
//                                        @Override
//                                        public void onConfirm(Object o) {
//                                            sModel.updateQuotePrice(mCurPos, memberBean.getCompanyNo(), entity, (String) o, position);
//                                        }
//                                    })).show();
//                                }
//                            })).show();
                        }
                    }
                });
                break;
            case 2: // "工程工队"
                mTeamAdapter = new CompanyWebBusinessesAdapter<>(getActivity(), R.layout.item_company_web_team, null, isDemandMember);
                mBinding.recyclerView.setAdapter(mTeamAdapter);
                mTeamAdapter.setOnItemClick(new AdapterOnItemClick<MemberCompanyTeamBean>() {
                    @Override
                    public void onItemClick(MemberCompanyTeamBean bean, int position) {
                        sModel.getTeamH5Url(bean.getNo());
                    }
                });
                mTeamAdapter.setModifyQuoteListener(new CompanyWebBusinessesAdapter.ModifyQuoteListener<MemberCompanyTeamBean>() {
                    @Override
                    public void onModifyQuote(int position, MemberCompanyTeamBean entity) {
                        if (getActivity() != null) {
                            Bundle bundle = new Bundle();
                            bundle.putParcelable("bean", entity);
                            bundle.putString("no", memberBean.getCompanyNo());
                            MemberBusinessPriceActivity3.Companion.jumptToAct(requireActivity(), bundle);
//                            new XPopup.Builder(getContext()).asCustom(new MemberBusinessPopup(getActivity(), new MemberBusinessPopup.OnConfirmListener() {
//                                @Override
//                                public void onConfirm() {
//                                    new XPopup.Builder(getContext()).asCustom(new MemberBusinessPricePopup(getActivity(), mCurPos, entity, new MemberBusinessPricePopup.OnConfirmListener() {
//                                        @Override
//                                        public void onConfirm(Object o) {
//                                            sModel.updateQuotePrice(mCurPos, memberBean.getCompanyNo(), entity, (String) o, position);
//                                        }
//                                    })).show();
//                                }
//                            })).show();
                        }
                    }
                });
                break;
        }
        mBinding.emptyView.findViewById(R.id.iv_null).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtils.isDoubleClick()) {
                    loadData(false);
                }
            }
        });
    }

    private void loadData(boolean isLoadMore) {
        sModel.getBusinessData(flag,mCurPos, isLoadMore, memberBean, mNo1, mNo2, mNo3);
    }

    public void closeRecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    public void showEmpty() {
        mBinding.recyclerView.setVisibility(View.GONE);
        mBinding.emptyView.setVisibility(View.VISIBLE);
    }

    public <T> void refreshRecycler(List<T> list, boolean isLoadMore, boolean noMoreData) {
        switch (mCurPos) {
            case 0: // "经营商品"
                refreshRecycler(mBusinessAdapter, list, isLoadMore, noMoreData);
                break;
            case 1: // "租赁经营"
                refreshRecycler(mLeaseAdapter, list, isLoadMore, noMoreData);
                break;
            case 2: // "工程工队"
                refreshRecycler(mTeamAdapter, list, isLoadMore, noMoreData);
                break;
        }
    }

    public <T> void refreshRecycler(BaseRecycleAdapter adapter, List<T> list, boolean isLoadMore, boolean noMoreData) {
        closeRecyclerRefreshing();
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setLoadingMoreEnabled(!noMoreData);
        if (adapter == null) {
            return;
        }
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                adapter.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
            } else {
                adapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    // 修改报价成功
    public void updateQuotePriceCallback(int updateIndex, String price) {
        switch (mCurPos) {
            case 0: // "经营商品"
                mBusinessAdapter.getDatas().get(updateIndex).setHasQuete(true);
                mBusinessAdapter.getDatas().get(updateIndex).setQuetePrice(Double.parseDouble(price));
                mBusinessAdapter.notifyDataSetChanged();
                break;
            case 1: // "租赁经营"
                mLeaseAdapter.getDatas().get(updateIndex).setQuetePrice(Double.parseDouble(price));
                mLeaseAdapter.notifyDataSetChanged();
                break;
            case 2: // "工程工队"
                mTeamAdapter.getDatas().get(updateIndex).setQuetePrice(Double.parseDouble(price));
                mTeamAdapter.notifyDataSetChanged();
                break;
        }
    }

    public void setTablayout1(List<CommonPopEntity> data) {
        mBinding.tabLayout1.removeAllTabs();
        mBinding.tabLayout1.clearOnTabSelectedListeners();
        mBinding.tabLayout1.setNormalColor(R.color.color_title_small);
        mBinding.tabLayout1.setSelectColor(R.color.color_main);
        mBinding.tabLayout1.setNormalSize(14);
        mBinding.tabLayout1.setSelectSize(14);
        mBinding.tabLayout1.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < data.size(); i++) {
            mBinding.tabLayout1.addTab(mBinding.tabLayout1.newTab().setText(data.get(i).getTypeName()));
        }
        mBinding.tabLayout1.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(mCurPos==0){
                    mNo1 = data.get(tab.getPosition()).getId();
                }else {
                    mNo1 = data.get(tab.getPosition()).getNo();
                }

                mNo2 = "";
                mNo3 = "";
                if (mCurPos != 2) {
                    sModel.getCategoryChoose(tab.getPosition());
                }
                loadData(false);
            }
        });
    }

    public void setTablayout2(List<CommonPopEntity> data) {
        if (data.size() > 0) {
            mBinding.tabLayout2.setVisibility(View.VISIBLE);
            mBinding.tabLayout3.setVisibility(View.GONE);
        } else {
            mBinding.tabLayout2.setVisibility(View.GONE);
            mBinding.tabLayout3.setVisibility(View.GONE);
            return;
        }
        mBinding.tabLayout3.setVisibility(View.GONE);
        mBinding.tabLayout2.removeAllTabs();
        mBinding.tabLayout2.clearOnTabSelectedListeners();
        mBinding.tabLayout2.setNormalColor(R.color.color_title_small);
        mBinding.tabLayout2.setSelectColor(R.color.color_main);
        mBinding.tabLayout2.setNormalSize(14);
        mBinding.tabLayout2.setSelectSize(14);
        mBinding.tabLayout2.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < data.size(); i++) {
            mBinding.tabLayout2.addTab(mBinding.tabLayout2.newTab().setText(data.get(i).getTypeName()));
        }
        mBinding.tabLayout2.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(mCurPos==0){
                    mNo2 = data.get(tab.getPosition()).getId();
                }else {
                    mNo2 = data.get(tab.getPosition()).getNo();
                }
                mNo3 = "";
                sModel.getCateArguments(tab.getPosition());
                loadData(false);
            }
        });
    }


    public void setTablayout3(List<CommonPopEntity> data) {
        if (data.size() > 0) {
            mBinding.tabLayout3.setVisibility(View.VISIBLE);
        } else {
            mBinding.tabLayout3.setVisibility(View.GONE);
            return;
        }
        mBinding.tabLayout3.removeAllTabs();
        mBinding.tabLayout3.clearOnTabSelectedListeners();
        mBinding.tabLayout3.setNormalColor(R.color.color_title_small);
        mBinding.tabLayout3.setSelectColor(R.color.color_main);
        mBinding.tabLayout3.setNormalSize(14);
        mBinding.tabLayout3.setSelectSize(14);
        mBinding.tabLayout3.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < data.size(); i++) {
            mBinding.tabLayout3.addTab(mBinding.tabLayout3.newTab().setText(data.get(i).getTypeName()));
        }
        mBinding.tabLayout3.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(mCurPos==0){
                    mNo3 = data.get(tab.getPosition()).getId();
                }else {
                    mNo3 = data.get(tab.getPosition()).getNo();
                }
                loadData(false);
            }
        });
    }
}
